package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjStoreGoodsStock;
import com.zb.entity.ZjStoreGoodsStockCode;
import com.zb.extend.StoreGoodsStockExtend;
import com.zb.mapper.ZjStoreGoodsStockMapper;
import com.zb.service.ZjStoreGoodsStockCodeService;
import com.zb.service.ZjStoreGoodsStockService;
import com.zb.vo.GoodsStockSelectVo;
import com.zb.vo.StoreGoodsStockVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 仓库商品库存表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-30
 */
@Service
public class ZjStoreGoodsStockServiceImpl extends ServiceImpl<ZjStoreGoodsStockMapper, ZjStoreGoodsStock> implements ZjStoreGoodsStockService {
    @Resource
    private ZjStoreGoodsStockMapper storeGoodsStockMapper;
    @Resource
    private ZjStoreGoodsStockCodeService storeGoodsStockCodeService;

    // 保存新增的仓库商品库存（注：此执行要重新执行查询，避免同时出入库导致物料重复）
    @Override
    public synchronized void save(List<ZjStoreGoodsStock> storeGoodsStockList, List<ZjStoreGoodsStockCode> storeGoodsStockCodeList) {
        List<Long> storeIds = storeGoodsStockList.stream().map(ZjStoreGoodsStock::getStoreId).distinct().toList();
        List<ZjStoreGoodsStock> goodsStockList = storeGoodsStockMapper.getByStoreIds(storeIds);

//        List<Long> storeGoodIds = storeGoodsStockList.stream().map(ZjStoreGoodsStock::getStoreGoodsId).distinct().toList();
//        List<ZjStoreGoodsStock> goodsStockList = storeGoodsStockMapper.getByStroreIdsAndStorePositionIdsAndStoreGoodsIds(storeIds, storeGoodIds);
        List<ZjStoreGoodsStock> adds = new ArrayList<>();
        List<ZjStoreGoodsStock> updates = new ArrayList<>();
        ZjStoreGoodsStock goodsStock = null;
        for (ZjStoreGoodsStock stock : storeGoodsStockList) {
            if (null == stock.getStorePositionId()) {
                stock.setStorePositionId(0L);
            }
            goodsStock = goodsStockList.stream().filter(f -> Objects.equals(f.getStoreId(), stock.getStoreId()) && Objects.equals(f.getStorePositionId(), stock.getStorePositionId()) &&
                    Objects.equals(f.getGoodsType(), stock.getGoodsType()) && Objects.equals(f.getGoodsId(), stock.getGoodsId()) && Objects.equals(f.getGoodsSpecId(), stock.getGoodsSpecId()))
                    .findFirst().orElse(null);
            if (null != goodsStock) {
                if (CollectionUtil.isNotEmpty(storeGoodsStockCodeList)) {
                    for (ZjStoreGoodsStockCode code : storeGoodsStockCodeList) {
                        if (Objects.equals(code.getStoreGoodsStockId(), stock.getId())) {
                            code.setStoreGoodsStockId(goodsStock.getId());
                        }
                    }
                }
                stock.setSystemStock(new BigDecimal(goodsStock.getSystemStock().toString()).add(new BigDecimal(stock.getSystemStock().toString())).longValue());
                stock.setId(goodsStock.getId());
                updates.add(stock);
            } else {
                adds.add(stock);
            }
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
        // 仓库商品库存一物一码
        storeGoodsStockCodeService.saveOrUpdateBatchById(storeGoodsStockCodeList);
    }

    // 根据仓库ID删除仓库商品库存
    @Override
    public void deleteBatchByStoreId(List<Long> storeIds) {
        if (CollectionUtil.isEmpty(storeIds)) {
            return;
        }
        QueryWrapper<ZjStoreGoodsStock> wrapper = new QueryWrapper<>();
        wrapper.in("store_id", storeIds);
        this.remove(wrapper);
    }

    // 根据[商品ID]查询是否与仓库物料仓库关联的商品ID
    @Override
    public List<Long> getGoodsIdByGoodsIds(Integer type, List<Long> goodsIds) {
        if (CollectionUtil.isEmpty(goodsIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreGoodsStock> wrapper = new QueryWrapper<>();
        wrapper.select( "goods_id");
        wrapper.eq("goods_type", type).in("goods_id", goodsIds);
        List<ZjStoreGoodsStock> goodsStockList = this.list(wrapper);
        if (CollectionUtil.isEmpty(goodsStockList)) {
            return new ArrayList<>();
        }
        return goodsStockList.stream().map(ZjStoreGoodsStock::getGoodsId).distinct().toList();
    }

    // 根据[商品ID]&[商品规格ID]查询是否与仓库物料仓库关联的商品规格ID
    @Override
    public List<Long> getGoodsSpecIdByGoodsIdAndGoodSpecIds(Integer type, Long goodsId, List<Long> goodSpecIds) {
        if (CollectionUtil.isEmpty(goodSpecIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreGoodsStock> wrapper = new QueryWrapper<>();
        wrapper.select( "goods_spec_id");
        wrapper.eq("goods_type", type)
                .eq("goods_id", goodsId)
                .in("goods_spec_id", goodSpecIds);
        List<ZjStoreGoodsStock> goodsStockList = this.list(wrapper);
        if (CollectionUtil.isEmpty(goodsStockList)) {
            return new ArrayList<>();
        }
        return goodsStockList.stream().map(ZjStoreGoodsStock::getGoodsSpecId).distinct().toList();
    }

    // 根据仓位ID集合分组统计获取即时库存数
    @Override
    public List<ZjStoreGoodsStock> getSystemStockGroupByStorePositionIds(List<Long> storePositionIds) {
        if (CollectionUtil.isEmpty(storePositionIds)) {
            return new ArrayList<>();
        }
        return storeGoodsStockMapper.getSystemStockGroupByStorePositionIds(storePositionIds);
    }

    // 根据[仓库ID]&[商品ID]&[商品规格ID]查询仓库商品库存
    @Override
    public List<ZjStoreGoodsStock> getByStoreIdsAndGoodsIdsAndGoodsSpecIds(List<Long> storeIds, List<Long> goodsIds, List<Long> goodsSpecIds) {
        if (CollectionUtil.isEmpty(storeIds) || CollectionUtil.isEmpty(goodsIds) || CollectionUtil.isEmpty(goodsSpecIds)) {
            return new ArrayList<>();
        }
        return storeGoodsStockMapper.getByStoreIdsAndGoodsIdsAndGoodsSpecIds(storeIds, goodsIds, goodsSpecIds);
    }

    // 获取商品库存
    @Override
    public StoreGoodsStockVo getGoodsStock(GoodsStockSelectVo selectVo) {
        ZjStoreGoodsStock storeGoodsStock = storeGoodsStockMapper.getGoodsStock(selectVo);
        if (null == storeGoodsStock) {
            return new StoreGoodsStockVo(0L, 0, new BigDecimal("0.0"));
        }
        return new StoreGoodsStockVo(storeGoodsStock.getId(), storeGoodsStock.getSystemStock(), storeGoodsStock.getPrice());
    }

    // 根据仓库ID获取仓库商品库存
    @Override
    public List<ZjStoreGoodsStock> getByStoreIds(List<Long> storeIds) {
        if (CollectionUtil.isEmpty(storeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreGoodsStock> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_id", "store_position_id", "goods_type", "goods_id", "goods_spec_id", "system_stock")
                .in("store_id", storeIds);
        return this.list(wrapper);
    }

    // 根据仓库ID获取仓库商品库存
    @Override
    public List<ZjStoreGoodsStock> getByStoreId(Long storeId) {
        List<Long> storeIds = new ArrayList<>();
        storeIds.add(storeId);
        return getByStoreIds(storeIds);
    }

    // 获取服务商仓库商品明细列表
    @Override
    public List<StoreGoodsStockExtend> getNetworkMobileStoreGoods(Long storeId, SelectVo selectVo) {
        return storeGoodsStockMapper.getNetworkMobileStoreGoods(storeId, selectVo);
    }

    // 获取服务商仓库商品明细列表总记录数
    @Override
    public long getNetworkMobileStoreGoodsCount(Long storeId, SelectVo selectVo) {
        return storeGoodsStockMapper.getNetworkMobileStoreGoodsCount(storeId, selectVo);
    }


}
